package com.wantupai.mvp.mvp.lce.activity;

import android.app.Dialog;
import android.os.Bundle;
import android.support.annotation.DrawableRes;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

import com.wantupai.mvp.framework.support.MvpDelegateCallback;
import com.wantupai.mvp.framework.support.activity.ActivityMvpDelegateImpl;
import com.wantupai.mvp.mvp.MvpPresenter;
import com.wantupai.mvp.mvp.MvpView;
import com.wantupai.mvp.mvp.lce.MvpViewImpl;


/**
 * 中介者模式->具体同事（抽象）->绑定V层和解绑V层
 * 代理模式->第一重代理->代理对象->生命周期(持有目标对象引用)->MvpActivity
 * 代理模式->第二重代理->目标对象->绑定和解绑->MvpActivity(实现目标接口)
 *
 * @param <V>
 * @param <P>
 * @author :zhangqing
 */
public class MvpActivity<D, V extends MvpView<D>, P extends MvpPresenter<V>> extends AppCompatActivity implements MvpView<D>, MvpDelegateCallback<V, P> {

    //目标对象
    protected ActivityMvpDelegateImpl<V, P> mvpDelegate;

    public ActivityMvpDelegateImpl<V, P> getMvpDelegate() {
        if (this.mvpDelegate == null) {
            this.mvpDelegate = new ActivityMvpDelegateImpl<V, P>(this, this, true);
        }
        return mvpDelegate;
    }

    /***********第二重代理****start*******/
    private P presenter;

    @Override
    public void onContentChanged() {
        super.onContentChanged();
        getMvpLceView().initView(getWindow().getDecorView().findViewById(android.R.id.content));
        getMvpLceView().setOnClickErrorListener(this::onErrorClick);
        showContent();
    }

    public void Login(View view) {
        toLogin();
    }

    public void toLogin() {

    }

    protected MvpViewImpl<D> mDMvpViewImpl;


    public MvpViewImpl getMvpLceView() {
        if (mDMvpViewImpl == null) {
            mDMvpViewImpl = new MvpViewImpl<>();
        }
        return mDMvpViewImpl;
    }

    public void onErrorClick(View view) {
        loadData(false);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getMvpDelegate().onCreate(savedInstanceState);
    }

    @Override
    protected void onStart() {
        super.onStart();
        getMvpDelegate().onStart();
    }

    @Override
    protected void onResume() {
        super.onResume();
        getMvpDelegate().onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        getMvpDelegate().onPause();
    }

    @Override
    protected void onStop() {
        super.onStop();
        getMvpDelegate().onStop();
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        getMvpDelegate().onRestart();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        getMvpDelegate().onDestroy();
    }

    @Override
    public P createPresenter() {
        return presenter;
    }

    @Override
    public void setPresenter(P presenter) {
        this.presenter = presenter;
    }

    @Override
    public P getPresenter() {
        return presenter;
    }

    @Override
    public V getMvpView() {
        return (V) this;
    }


    @Override
    public void showLoading(boolean isPullToRefresh) {
        getMvpLceView().showLoading(isPullToRefresh);
    }

    @Override
    public void loadData(boolean isPullToRefresh) {
        getMvpLceView().loadData(isPullToRefresh);
    }

    @Override
    public void showContent() {
        getMvpLceView().showContent();
    }

    @Override
    public void bindData(D d) {
        getMvpLceView().bindData(d);
    }

    @Override
    public void showError() {
        getMvpLceView().showError();
    }

    @Override
    public void showEmpty() {
        getMvpLceView().showEmpty();
    }

    private Dialog mDialog;

    @Override
    public void showLoadingDialog() {
       /* if (mDialog == null)
        {
            mDialog = LoadingDialog.builder(this);
        }
        mDialog.show();*/
    }

    @Override
    public void dismissDialog() {
        if (mDialog != null) {
            mDialog.dismiss();
        }
    }

    public void setEmptyImageView(@DrawableRes int imageResourcesId) {
        getMvpLceView().setEmptyImageView(imageResourcesId);
    }

    public void setEmptyTextView(CharSequence charSequence) {
        getMvpLceView().setEmptyTextView(charSequence);
    }


    @Override
    public void showNetWorkError() {
        // new ToastDialog.Builder(this).setContent("网络不好，请检查您的网络设置").show();
    }
}
